package com.jswlw.common.utils;

import javax.servlet.ServletOutputStream;

import com.jswlw.entity.biz.DevInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.util.List;

/**
 * @author 吴维斌 E-mail:wuweibin0817@163.com
 * @version 创建时间：${date} ${time}
 */
public class ExcelUtils {

    public static void exportDevInfo(String[] titles, List<DevInfo> devInfoList, ServletOutputStream out) throws Exception {
        try {
            // 第一步，创建一个workbook，对应一个Excel文件
            HSSFWorkbook workbook = new HSSFWorkbook();

            // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet hssfSheet = workbook.createSheet("设备信息");
            hssfSheet.autoSizeColumn(0);
            hssfSheet.autoSizeColumn(1);
            hssfSheet.autoSizeColumn(2);
            hssfSheet.autoSizeColumn(3);
            hssfSheet.autoSizeColumn(4);
            hssfSheet.autoSizeColumn(5);
            hssfSheet.autoSizeColumn(6);
            hssfSheet.autoSizeColumn(7);
            hssfSheet.autoSizeColumn(8);


            // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short

            HSSFRow row = hssfSheet.createRow(0);
            // 第四步，创建单元格，并设置值表头 设置表头居中
            HSSFCellStyle hssfCellStyle = workbook.createCellStyle();

            HSSFCell hssfCell = null;
            for (int i = 0; i < titles.length; i++) {
                hssfCell = row.createCell(i);//列索引从0开始
                hssfCell.setCellValue(titles[i]);//列名1
                hssfCell.setCellStyle(hssfCellStyle);//列居中显示
            }

            for (int i = 0; i < devInfoList.size(); i++) {
                row = hssfSheet.createRow(i + 1);
                DevInfo devInfo = devInfoList.get(i);
                row.createCell(0).setCellValue(devInfo.getId());
                row.createCell(1).setCellValue(devInfo.getName());
                row.createCell(2).setCellValue(devInfo.getUserName());
                row.createCell(3).setCellValue(devInfo.getTel());
                row.createCell(4).setCellValue(devInfo.getAddress());
                row.createCell(5).setCellValue(devInfo.getDistributorId());
                row.createCell(6).setCellValue(devInfo.getDistributorName());
                String gzms = "";
                if (devInfo.getBillTypeId() == 0) {
                    gzms = "流量";
                } else if (devInfo.getBillTypeId() == 2) {
                    gzms = "时长";
                } else {
                    gzms = "买断";
                }
                row.createCell(7).setCellValue(gzms);
                row.createCell(8).setCellValue(devInfo.getImei());
            }

            // 第七步，将文件输出到客户端浏览器
            try {
                workbook.write(out);
                out.flush();
                out.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("导出信息失败！");

        }
    }
}
